﻿#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<stack>
#include<functional>
using namespace std;

// 类模板原型如下
//template <class T> function;     // undefined
//template <class Ret, class... Args>   
//class function<Ret(Args...)>;    Ret是返回类型，（这里面是参数类型）




template<class F, class T>
T useF(F y, T x)
{
	static int count = 0;
	cout << "count:" << ++count << endl;
	cout << "count:" << &count << endl;
	return y(x);
}
double f(double i)
{
	return i / 2;
}
struct Functor
{
	double operator()(double d)
	{
		return d / 3;
	}
};
//int main()
//{
//	// 函数名
//	cout << useF(f, 11.11) << endl;
//	// 函数对象
//	cout << useF(Functor(), 11.11) << endl;
//	// lamber表达式
//	cout << useF([](double d)->double { return d / 4; }, 11.11) << endl;
//	return 0;
//}
//int main()
//{
//	// 函数名
//	function<double(double)> fuc1 = f;
//	cout << useF(fuc1, 11.11) << endl;
//	// 函数对象
//	function<double(double)> fuc2 = Functor();
//	cout << useF(fuc2, 11.11) << endl;
//	
//	// lamber表达式
//	function<double(double)> fuc3 = [](double d)->double { return d / 4; };
//	cout << useF(fuc3, 11.11) << endl;
//	return 0;
//}


//力扣：150. 逆波兰表达式求值

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        map<string, function<int(int, int)>> funmap{//用function包装器解决
            {"+",[](int x,int y) {return x + y; }},
            {"-",[](int x,int y) {return x - y; }},
            {"*",[](int x,int y) {return x * y; }},
            {"/",[](int x,int y) {return x / y; }},
        };
        stack<int> s;
        for (auto& e : tokens)
        {
            if (funmap.count(e))
            {
                int right = s.top();
                s.pop();
                int left = s.top();
                s.pop();
                s.push(funmap[e](left, right));
            }
            else {
                s.push(stoi(e));
            }
        }
        return s.top();
    }
};




class Plus
{
public:
    static int plusi(int a, int b)
    {
        return a + b;
    }
    double plusd(double a, double b)
    {
        return a + b;
    }
};

int main()
{
    std::function<int(int, int)> func4 = &Plus::plusi;
    cout << func4(1, 2) << endl;
   /* std::function<double(Plus, double, double)> func5 = &Plus::plusd;
    cout << func5(Plus(), 1.1, 2.2) << endl;*/
    Plus pp;
    std::function<double(Plus&, double, double)> func5 = &Plus::plusd;
    cout << func5(pp, 1.1, 2.2) << endl;
    //使用bind绑定
    std::function<double(double, double)> func6 = bind(&Plus::plusd,Plus(), placeholders::_1, placeholders::_2);
    cout << func6(1.1, 2.2) << endl;
    return 0;
}


int Sub(int x, int y)
{
    return x - y;
}
//int main()
//{
//    //bind;  可以调整参数顺序     可以改变参数个数；
//    auto fuc1 = bind(Sub, placeholders::_2, placeholders::_1);
//    cout << fuc1(5, 7) << endl;
//
//
//    //改变参数个数
//    auto fuc2 = bind(Sub, placeholders::_1,10 );
//    cout << fuc2(7) << endl;
//    return 0;
//}